package com.yqy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqy.entity.Project;
import com.yqy.entity.ProjectPo;
import com.yqy.mapper.ProjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author yiqianyi
 * @date 2022/3/22 19:07
 */
@CrossOrigin
@RestController
@Api(tags = {"项目控制层"})
public class ProjectController {
    @Autowired
    private ProjectMapper projectMapper;

    @PostMapping("/project/getPage")
    @ApiOperation(value = "分页获取项目信息", notes = "/project/getPage")
    public IPage<Project> getPageStudentInfo(@RequestParam @ApiParam(name = "pageNum", value = "pageNum", required = true) int pageNum, @RequestParam @ApiParam(name = "pageSize", value = "pageSzie", required = true) int pageSize, @RequestBody @ApiParam(name = "project", value = "project", required = true) Project project) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (!("").equals(project.getProjectName())) {
            queryWrapper.like("projectName", project.getProjectName());
        }
        if (!("").equals(project.getProjectBelong())) {
            queryWrapper.eq("projectBelong", project.getProjectBelong());
        }
        Page<Project> studentInfoPage = new Page<>(pageNum, pageSize);
        return projectMapper.selectPage(studentInfoPage, queryWrapper);
    }

    @PostMapping("/project/update")
    @ApiOperation(value = "修改项目消息", notes = "/project/update")
    public int update(@RequestBody @ApiParam(name = "project", value = "project", required = true) Project project) {
        return projectMapper.updateById(project);
    }

    @PostMapping("/project/insert")
    @ApiOperation(value = "新增项目消息", notes = "/project/insert")
    public int insert(@RequestBody @ApiParam(name = "project", value = "project", required = true) Project project) {
        return projectMapper.insert(project);
    }

    @PostMapping("/project/delete")
    @ApiOperation(value = "批量删除项目信息", notes = "/project/delete")
    public int delete(@RequestParam @ApiParam(name = "ids", value = "1,2,3", required = true) String ids) {
        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);
        return projectMapper.deleteBatchIds(list);
    }

    @GetMapping("/project/cascade")
    @ApiOperation(value = "获取项目级联信息", notes = "/project/cascade")
    public List<ProjectPo> cascade() {
        List<ProjectPo> projectBelongList = projectMapper.groupByBelong();
        for (int i =0;i<projectBelongList.size();i++){
            projectBelongList.get(i).setLabel(projectBelongList.get(i).getProjectBelong());
            projectBelongList.get(i).setValue(projectBelongList.get(i).getProjectBelong());
        }
        List<Project> name = projectMapper.selectList(new QueryWrapper());

//        List<ProjectPo> projectBelongList = new ArrayList<>();
//        for (Project project : belong) {
//            ProjectPo projectPo = new ProjectPo();
//            projectPo.setId(project.getId());
//            projectPo.setProjectBelong(project.getProjectBelong());
//            projectPo.setProjectName(project.getProjectName());
//            projectBelongList.add(projectPo);
//        }
        List<ProjectPo> projectNameList = new ArrayList<>();
        for (Project project : name) {
            ProjectPo projectPo = new ProjectPo();
            projectPo.setId(project.getId());
            projectPo.setProjectBelong(project.getProjectBelong());
            projectPo.setProjectName(project.getProjectName());
            projectNameList.add(projectPo);
        }

        for (int i = 0; i < projectBelongList.size(); i++) {
            List<Map> list = new ArrayList<>();
            for (int j = 0; j < projectNameList.size(); j++) {
                Map map = new HashMap(16);
                if (projectBelongList.get(i).getProjectBelong().equals(projectNameList.get(j).getProjectBelong())) {
                    map.put("label", projectNameList.get(j).getProjectName());
                    map.put("value", projectNameList.get(j).getProjectName());
                    list.add(map);
                }
            }
            projectBelongList.get(i).setChildren(list);
        }
        return projectBelongList;
    }
}
